package cn.edu.sjtu.se.peakfactory.dao.impls.hibernate;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import cn.edu.sjtu.se.lchang.socialnetwork.dao.impls.hibernate.BaseDAOImpl;
import cn.edu.sjtu.se.peakfactory.dao.bean.UserElement;
import cn.edu.sjtu.se.peakfactory.dao.interfaces.UserElementDAO;

public class UserElementDAOImpl extends BaseDAOImpl<UserElement> implements UserElementDAO {
	
	@Override
	public UserElement getUserElementById(Long userId, Long elementId) {
		DetachedCriteria criteria = DetachedCriteria.forClass(UserElement.class);
		criteria.add(Restrictions.eq("user.id", userId)).add(Restrictions.eq("element.id", elementId));
		List<UserElement> result = findByCriteria(criteria);
		if (result != null && !result.isEmpty()) {
			return result.get(0);
		}
		return null;
	}
	
	@Override
	public List<UserElement> getUserElementByUserId(Long userId){
		DetachedCriteria criteria = DetachedCriteria.forClass(UserElement.class);
		criteria.add(Restrictions.eq("user.id", userId));
		return findByCriteria(criteria);
	}
}
